package com.gomore.opple.widgets.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.gomore.opple.R;


public abstract class ReDialog extends Dialog implements View.OnClickListener {

    protected Bundle mArgs = null;
    protected Context mContext;
    protected View mDialogView = null;
    protected Button mLeftBtn = null;
    protected Button mRightBtn = null;
    private Button mSingleButton;
    private ViewGroup mSingleButtonLayout;
    private View mHorizontal_divider;

    enum DialogButtonsStyle {
        SINGLE_BUTTON, TWO_BUTTONS, OTHER, LOADING
    }

    public ReDialog(Context paramContext, int paramInt, Bundle paramBundle, View DialogView, DialogButtonsStyle paramDialogButtonsStyle) {
        super(paramContext, R.style.gomore_dialog);
        this.mContext = paramContext;
        if (DialogView != null) {
            this.mDialogView = DialogView;
        } else if (DialogView == null) {
            LayoutInflater mLayoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mDialogView = mLayoutInflater.inflate(paramInt, null);
        }
        setArgs(paramBundle);
        init(paramDialogButtonsStyle);
    }

    protected void closeDialog() {
        this.mContext = null;
        if (getOwnerActivity() != null) {
            getOwnerActivity().removeDialog(getDialogId());
        }
        dismiss();
    }

    protected void init(DialogButtonsStyle paramDialogButtonsStyle) {
        Window localWindow = getWindow();
        localWindow.setBackgroundDrawableResource(android.R.color.transparent);
        localWindow.requestFeature(Window.FEATURE_NO_TITLE);
        localWindow.setGravity(Gravity.TOP);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        switch (paramDialogButtonsStyle) {
            case TWO_BUTTONS:
//			initLeftRightButtons(this.mDialogView);
                break;
            case SINGLE_BUTTON:
//			initSingleButton(this.mDialogView);
                break;
            case OTHER:
                break;
            case LOADING:
                setCancelable(true);
                break;
        }
        setContentView(this.mDialogView);
        getWindow().getAttributes().y = (int) TypedValue.applyDimension(1, 10.0f, mContext.getResources().getDisplayMetrics());
    }

    protected abstract int getDialogId();

    @Override
    public void onClick(View v) {
        closeDialog();
    }

//	protected void initLeftRightButtons(View paramView) {
//		this.mLeftBtn = (Button) paramView.findViewById(R.id.leftBtn);
//		this.mRightBtn = (Button) paramView.findViewById(R.id.rightBtn);
//		// 设置字体
//		 Typeface localTypeface1 =
//		 ThemeManager.getFont(ThemeAttribute.GENERAL_FONT_DIN_DISPLAY_THIN);
//		 this.mLeftBtn.setTypeface(localTypeface1);
//		 Typeface localTypeface2 =
//		 ThemeManager.getFont(ThemeAttribute.GENERAL_FONT_DIN_DISPLAY_LIGHT);
//		 this.mRightBtn.setTypeface(localTypeface2);localTypeface2
//		setRightBtnListener(this);
//	}

//	protected void initSingleButton(View paramView) {
//		this.mHorizontal_divider = (View) paramView
//				.findViewById(R.id.horizontal_divider);
//		this.mSingleButton = (Button) paramView
//				.findViewById(R.id.single_button);
//		this.mSingleButtonLayout = (ViewGroup) paramView
//				.findViewById(R.id.single_button_layout);
//		// 设置字体
//		// Typeface localTypeface =
//		// ThemeManager.getFont(ThemeAttribute.GENERAL_FONT_DIN_DISPLAY_THIN);
//		// this.mSingleButton.setTypeface(localTypeface);
//		setSingleButtonListener(this);
//	}

    public void setArgs(Bundle paramBundle) {
        this.mArgs = paramBundle;
    }

    public void setLeftBtnListener(View.OnClickListener paramOnClickListener) {
        this.mLeftBtn.setOnClickListener(paramOnClickListener);
    }

    public void setRightBtnListener(View.OnClickListener paramOnClickListener) {
        this.mRightBtn.setOnClickListener(paramOnClickListener);
    }

    public void setSingleButtonListener(
            View.OnClickListener paramOnClickListener) {
        this.mSingleButton.setOnClickListener(paramOnClickListener);
        this.mSingleButtonLayout.setOnClickListener(paramOnClickListener);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public abstract void onClose();

    public void setLeftBtnText(int strID) {
        this.mLeftBtn.setText(strID);
    }

    public void setLeftBtnText(String str) {
        this.mLeftBtn.setText(str);
    }

    public void setRightBtnText(int strID) {
        this.mRightBtn.setText(strID);
    }

    public void setRightBtnText(String str) {
        this.mRightBtn.setText(str);
    }

    public void setSingleBtnText(int strID) {
        this.mSingleButton.setText(strID);
    }

    public void setSingleBtnText(String str) {
        this.mSingleButton.setText(str);
    }

    /**
     * 隐藏确认按钮
     */
    public void setHideSingleButton() {
        this.mHorizontal_divider.setVisibility(View.GONE);
        this.mSingleButtonLayout.setVisibility(View.GONE);
    }

    @Override
    public void dismiss() {
        try {
            super.dismiss();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public boolean isContextRelease() {
        return mContext == null || getContext() == null;
    }

}
